//
//  LoadData.swift
//  NewCard
//
//  Created by yihui on 2017/11/17.
//  Copyright © 2017年 yihui. All rights reserved.
//

import UIKit

enum dataType {
    case User
    case Card
}

class OpenData {
    
    // 查询数据
    class func loadData(_ type : dataType) -> [[String : AnyObject]]? {
        if type == .User || type == .Card  {
            var querySQL: String?
            switch type {
                case .User:
                    querySQL = "SELECT * FROM UserDetails WHERE id = 1;"
                case .Card:
                    querySQL = "SELECT * FROM CardDetails;"
            }
            return SQLiteManager.shareInstance().querySQL(querySQL!)
        }
        return nil
    }
    
    class func loadData(ExpenseStartAndDateEndDate data : ExpenseModel) -> [[String : AnyObject]]? {
        let querySQL = "SELECT * FROM ExpenseDetails WHERE UseType = '消费' AND BankName = '\(data.bankName!)' AND BankCard = \(data.bankCard) AND date >= '\(data.startDate!)' AND date <= '\(data.endDate!)';"
        return SQLiteManager.shareInstance().querySQL(querySQL)
    }
    
    class func loadData(ExpenseListSelectData data : CardModel) -> [[String : AnyObject]]? {
        let querySQL = "SELECT * FROM ExpenseDetails WHERE BankName = '\(data.bankName!)' AND BankCard = \(data.bankCard);"

        return SQLiteManager.shareInstance().querySQL(querySQL)
    }
    
    class func loadData(CardDetailsSelectData data : CardModel) -> [[String : AnyObject]]? {
        let querySQL = "SELECT * FROM CardDetails WHERE BankName = '\(data.bankName!)' AND BankCard = \(data.bankCard);"
        return SQLiteManager.shareInstance().querySQL(querySQL)
    }
    
    // 写入数据
    class func insertData(data : UserModel) -> Bool{
        // 1.封装插入的SQL
        let insertSQL =  "INSERT INTO UserDetails (PassWord, Problem, Answer) VALUES ('\(data.passWord!)', '\(data.problem!)', '\(data.answer!)');"
        // 2.执行SQL
        return SQLiteManager.shareInstance().execSQL(insertSQL)
    }
    class func insertData(data : CardModel) -> Bool{
        // 1.封装插入的SQL
        let insertSQL =  "INSERT INTO CardDetails (BankName, BankCard, Total, TemporaryAmount, UsableAmount, BillDate, ReturnDate) VALUES ('\(data.bankName!)', \(data.bankCard), \(data.total), \(data.temporaryAmount), \(data.usableAmount), \(data.billDate), \(data.returnDate));"
        // 2.执行SQL
        return SQLiteManager.shareInstance().execSQL(insertSQL)
    }
    class func insertData(data : ExpenseModel) -> Bool{
        // 1.封装插入的SQL
        let insertSQL =  "INSERT INTO ExpenseDetails (BankName, BankCard, UseType, Money, Note) VALUES ('\(data.bankName!)', \(data.bankCard), '\(data.useType!)', \(data.money), '\(data.note!)');"
        // 2.执行SQL
        return SQLiteManager.shareInstance().execSQL(insertSQL)
    }
    
    // 修改数据
    class func updateData(data : UserModel) -> Bool {
        let updateSQL = "UPDATE UserDetails SET PassWord = '\(data.passWord!)', Problem = '\(data.problem!)', Answer = '\(data.answer!)' WHERE id = 1;"
        return SQLiteManager.shareInstance().execSQL(updateSQL)
    }
    
    class func updateData(data : CardModel) -> Bool {
        let updateSQL = "UPDATE CardDetails SET BankName = '\(data.bankName!)', BankCard = \(data.bankCard), Total = \(data.total), TemporaryAmount = \(data.temporaryAmount), UsableAmount = \(data.usableAmount), BillDate = \(data.billDate), ReturnDate = \(data.returnDate) WHERE id = \(data.id);"
        return SQLiteManager.shareInstance().execSQL(updateSQL)
    }
    
    // 删除数据
    class func deleteData(data : CardModel) -> Bool {
        // 1.封装删除的SQL
        var deleteSQL = "DELETE FROM CardDetails WHERE id = \(data.id);"
        deleteSQL += "DELETE FROM ExpenseDetails WHERE BankCard = \(data.bankCard);"
        // 2.执行sql
        return SQLiteManager.shareInstance().execSQL(deleteSQL)
    }
    
    class func deleteData(data : ExpenseModel) -> Bool {
        // 1.封装删除的SQL
        let deleteSQL = "DELETE FROM ExpenseDetails WHERE id = \(data.id);"
        // 2.执行sql
        return SQLiteManager.shareInstance().execSQL(deleteSQL)
    }
    
}
